package educrypt.database.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import educrypt.commons.Text;
import educrypt.database.Connector;

public class TextDAO extends Observable { // It is used only inside
											// FrequencyAnalysisDAO

	/* Singleton */
	private TextDAO() {
	}

	public static TextDAO getInstance() {
		return SingletonHolder.instance;
	}

	private static class SingletonHolder {
		public static final TextDAO instance = new TextDAO();
	}

	/* END Singleton */

	public int create(Text text) {
		final String sqlCreateText = "INSERT INTO Text (name,hash,textType,textLanguage) VALUES (?,?,?,?)";
		int idNewText = findId(text.getContentHash());
		boolean textFound = idNewText != -1;
		if (!textFound) {
			Connection conn;
			try {
				conn = Connector.getConnection();
				PreparedStatement ps = conn.prepareStatement(sqlCreateText,
						Statement.RETURN_GENERATED_KEYS);
				int psIndex = 1;
				ps.setString(psIndex++, text.getName());
				ps.setString(psIndex++, text.getContentHash());
				ps.setString(psIndex++, text.getType());
				ps.setString(psIndex++, text.getLanguage());
				ps.executeUpdate();
				ResultSet rs = ps.getGeneratedKeys();
				if (rs.next()) {
					idNewText = rs.getInt(1);
				}
				rs.close();
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			this.setChanged();
			this.notifyObservers();
		}
		return idNewText;
	}

	public int findId(String hash) {
		final String sqlFindText = "SELECT * FROM Text WHERE hash='" + hash
				+ "'";
		int idText = -1;
		try {
			Connection conn = Connector.getConnection();
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sqlFindText);
			if (rs.next()) {
				idText = rs.getInt("id");
			}
			rs.close();
			st.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return idText;
	}

	public List<Text> findAll() {
		final String sqlFindAllTexts = "SELECT * FROM Text ORDER BY NAME";
		List<Text> texts = new ArrayList<Text>();
		try {
			Connection conn = Connector.getConnection();
			PreparedStatement ps = conn.prepareStatement(sqlFindAllTexts);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Text t = new Text();
				t.setName(rs.getString("name"));
				t.setType(rs.getString("textType"));
				t.setLanguage(rs.getString("textLanguage"));
				t.setHash(rs.getString("hash"));
				texts.add(t);
			}
			rs.close();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return texts;
	}

	public void delete(String hash) {
		final String sqlDeleteText = "DELETE FROM Text WHERE hash=?";
		try {
			Connection conn = Connector.getConnection();
			PreparedStatement ps = conn.prepareStatement(sqlDeleteText);
			ps.setString(1, hash);
			ps.executeUpdate();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		this.setChanged();
		this.notifyObservers();
	}
}
